#include "bsp_key.h"
#include "bsp_led.h"
#include "bsp_beep.h"
#include "bsp_int.h"
#include "bsp_gpio.h"
#include "bsp_epit.h"

void key_init()
{
    IOMUXC_SetPinMux(IOMUXC_SNVS_SNVS_TAMPER1_GPIO5_IO01,0);
    IOMUXC_SetPinConfig(IOMUXC_SNVS_SNVS_TAMPER1_GPIO5_IO01,0x30c0);
    GPIO5->GDIR &= ~(1<<1); 
}
uint8_t read_key()
{
    uint8_t re=0;
    uint32_t value = GPIO5->DR;
    value &= 1<<1;
    if(value==0)
        re=0;
    else
        re=1;
    return re;
}
void key_test()
{
    if(read_key())
    {
        led_ON();
        beep_ON();
    }
    else
    {
        led_OFF();
        beep_OFF();
    }
    
}

void key_irqhandler(unsigned int giccIar, void *userParam)
{
    //66000000(HZ)/66000000 (x)         频率取倒数为周期
    gpio_clearintflags(GPIO5,1);
    EPIT1_Init(0,66000000/100);//10ms中断一次
    EPIT1_ON();
}

void key_int_init()
{
    GIC_EnableIRQ(GPIO5_Combined_0_15_IRQn);
    gpio_intconfig(GPIO5,1,kGPIO_IntRisingEdge);
    gpio_enableint(GPIO5,1);
    system_register_irqhandler(GPIO5_Combined_0_15_IRQn,key_irqhandler,NULL);
}